'''
Created on 2009-11-18

@author: Jialai_Zhu
'''
import malnode
import through
import malxml
import mufs
import configure   
import os
from   downloader import urllib2Downloader 
def run(url): 
    print url
    if url[0] == '#':
        return
    dir = mufs.get_md5(url)
    tree = malxml.xmlmaltree(url, dir)
    fs = mufs.mufs(dir)
    fs.rmdir();
    try:
        hanlde = (fs.store_hanlde, tree.appendw)
        d = urllib2Downloader();
        through.through(malnode.node('root', url), hanlde, d)
        tree.toFile()
        zipname = configure.dir + os.sep + fs.md5(url) + '.zip'
        fs.zip(zipname)        
        fs.extract(zipname)
        fs.rmdir()
        os.removedirs(dir)   
        newpath = configure.dir + os.sep + "mufs" + os.sep + fs.md5(url) + '.zip'
        try:os.rename(zipname, newpath)
        except:os.remove(zipname)
             
                
    except Exception, ex:
        tree.toFile()
        print Exception, ':', ex
import os           
def runb(filename):
    filename = os.path.abspath(filename)
    f = file(filename, 'r')
    for line in f.readlines():
        run(line)
    f.close()
    
import getopt, sys
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "b:i:v", ["input="])
    except getopt.GetoptError:
        print getopt.GetoptError.msg
        sys.exit(2)
    for o, a in opts:
        if o in ("-i", "--input"):
            run(a)  
        if o in ("-b", "--file"):
            runb(a)      
    # ...
    
           
import string

if __name__ == "__main__":
    main()
#    run(r"http://www.37c.com.cn/institution/hospital/hospital03.asp?id=20669")
 
